#include<func.h>
//udp的服务器端
int main(int argc,char** argv)
{
    ARGS_CHECK(argc,3);
    int sfd=socket(AF_INET,SOCK_DGRAM,0);//初始化一个网络描述符，对应一个缓冲区
    ERROR_CHECK(sfd,-1,"socket");
    struct sockaddr_in ser_addr;
    bzero(&ser_addr,sizeof(ser_addr));//注意要清空
    ser_addr.sin_family=AF_INET;//标识采用ipv4通信
    ser_addr.sin_addr.s_addr=inet_addr(argv[1]);//将ip的点分十进制转换为网络字节序
    ser_addr.sin_port=htons(atoi(argv[2]));//把端口转换为网络字节序
    //开始绑定
    int ret=bind(sfd,(struct sockaddr*)&ser_addr,sizeof(ser_addr));//把ip和port绑定到对应的描述符上
    ERROR_CHECK(ret,-1,"bind");
    char buf[128]={0};
    struct sockaddr_in client_addr;//客户端的socket
    bzero(&client_addr,sizeof(client_addr));//注意要清空
    socklen_t addr_len=sizeof(client_addr);
    ret=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&client_addr,&addr_len);//服务器端先接收数据
    ERROR_CHECK(ret,-1,"recvfrom");
    printf("client ip=%s port=%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
    printf("%s\n",buf);
    ret=sendto(sfd,"world",5,0,(struct sockaddr*)&client_addr,sizeof(client_addr));//服务器端向客户端发送数据
    ERROR_CHECK(ret,-1,"sendto");
    close(sfd);
}